package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeCollectManager;
import com.stark.apkextract.lib.model.AeUtil;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ApkCollAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.databinding.ActivityApkCollBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class ApkCollActivity extends BaseAc<ActivityApkCollBinding> {

    private ApkCollAdapter collAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_apk_coll;
    }

    @Override
    protected void initView() {
        mDataBinding.ivApkCollBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvApkCollList.setLayoutManager(new GridLayoutManager(mContext, 2));
        collAdapter = new ApkCollAdapter();
        mDataBinding.rvApkCollList.setAdapter(collAdapter);
        collAdapter.addChildClickViewIds(R.id.tvApkCollItemPackName);
        collAdapter.setOnItemClickListener(this);
        collAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        ApkBean item = (ApkBean) adapter.getItem(position);
        ApkDetailsActivity.apkBean = item;
        startActivity(ApkDetailsActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.tvApkCollItemPackName://复制包名
                ClipboardUtils.copyText(collAdapter.getItem(position).pkgName);
                ToastUtils.showShort(R.string.copy_suc);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<ApkBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<ApkBean>> emitter) {
                List<String> listSys = AeCollectManager.getInstance().getCollectPackageNames();
                List<ApkBean> apkBeans = new ArrayList<>();
                ApkBean bean;
                for (String packageName : listSys) {
                    bean = new ApkBean();
                    bean.pkgName = packageName;
                    apkBeans.add(bean);
                }
                emitter.onNext(apkBeans);
            }

            @Override
            public void accept(List<ApkBean> packageNames) {
                if (!CollectionUtils.isEmpty(packageNames)) {
                    mDataBinding.rvApkCollList.setVisibility(View.VISIBLE);
                    collAdapter.setList(packageNames);
                } else {
                    mDataBinding.rvApkCollList.setVisibility(View.GONE);
                }
                dismissDialog(500L);
            }
        });
    }

}